package com.ruoyi.mining.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mining.mapper.ExamMapper;
import com.ruoyi.mining.domain.Exam;
import com.ruoyi.mining.service.IExamService;

/**
 * 小升初考试登记Service业务层处理
 *
 * @author mining
 * @date 2025-05-23
 */
@Service
public class ExamServiceImpl implements IExamService {
    @Autowired
    private ExamMapper examMapper;

    /**
     * 查询小升初考试登记
     *
     * @param id 小升初考试登记主键
     * @return 小升初考试登记
     */
    @Override
    public Exam selectExamById(Integer id) {
        return examMapper.selectExamById(id);
    }

    /**
     * 查询小升初考试登记列表
     *
     * @param exam 小升初考试登记
     * @return 小升初考试登记
     */
    @Override
    public List<Exam> selectExamList(Exam exam) {
        return examMapper.selectExamList(exam);
    }

    /**
     * 新增小升初考试登记
     *
     * @param exam 小升初考试登记
     * @return 结果
     */
    @Override
    public int insertExam(Exam exam) {
        return examMapper.insertExam(exam);
    }

    /**
     * 修改小升初考试登记
     *
     * @param exam 小升初考试登记
     * @return 结果
     */
    @Override
    public Exam updateExam(Exam exam) {
        trimExamFields(exam);
        int updateResult = 0;
        Exam examVo= examMapper.queryExamByApplySchoolAndIdcard(exam.getApplySchool() ,exam.getIdCard());
        exam.setStatus("未考");
        exam.setAttend("是");
        exam.setViewFlag(true);
        exam.setCreateDate(DateUtils.getCurrentTime());
        if(examVo==null ){
            updateResult = this.insertExam(exam);
        }else {
            updateResult = examMapper.updateExam(exam);
        }


        examVo= examMapper.queryExamByApplySchoolAndIdcard(exam.getApplySchool() ,exam.getIdCard());

        return examVo;


    }

    /**
     * 批量删除小升初考试登记
     *
     * @param ids 需要删除的小升初考试登记主键
     * @return 结果
     */
    @Override
    public int deleteExamByIds(Integer[] ids) {
        return examMapper.deleteExamByIds(ids);
    }

    /**
     * 删除小升初考试登记信息
     *
     * @param id 小升初考试登记主键
     * @return 结果
     */
    @Override
    public int deleteExamById(Integer id) {
        return examMapper.deleteExamById(id);
    }

    @Override
    public Exam queryExamByApplySchoolAndIdcard(String applySchool, String idCard) {
        return examMapper.queryExamByApplySchoolAndIdcard(applySchool , idCard);
    }

    private void trimExamFields(Exam exam) {
        if (exam.getApplySchool() != null) {
            exam.setApplySchool(exam.getApplySchool().trim());
        }
        if (exam.getChannel() != null) {
            exam.setChannel(exam.getChannel().trim());
        }
        if (exam.getName() != null) {
            exam.setName(exam.getName().trim());
        }
        if (exam.getPrimarySchool() != null) {
            exam.setPrimarySchool(exam.getPrimarySchool().trim());
        }
        if (exam.getSchoolArea() != null) {
            exam.setSchoolArea(exam.getSchoolArea().trim());
        }
        if (exam.getAddArea() != null) {
            exam.setAddArea(exam.getAddArea().trim());
        }
        if (exam.getIdCard() != null) {
            exam.setIdCard(exam.getIdCard().trim());
        }
        if (exam.getPhone1() != null) {
            exam.setPhone1(exam.getPhone1().trim());
        }
        if (exam.getPhone2() != null) {
            exam.setPhone2(exam.getPhone2().trim());
        }
        if (exam.getGender() != null) {
            exam.setGender(exam.getGender().trim());
        }
        if (exam.getLandSchool() != null) {
            exam.setLandSchool(exam.getLandSchool().trim());
        }
        if (exam.getCreateDate() != null) {
            exam.setCreateDate(exam.getCreateDate().trim());
        }
        if (exam.getGrade() != null) {
            exam.setGrade(exam.getGrade().trim());
        }
        if (exam.getAttend() != null) {
            exam.setAttend(exam.getAttend().trim());
        }
        if (exam.getStatus() != null) {
            exam.setStatus(exam.getStatus().trim());
        }
        if (exam.getExamNum() != null) {
            exam.setExamNum(exam.getExamNum().trim());
        }
    }
}
